package com.qf.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    // 声明业务Exchange
    @Bean
    public TopicExchange businessExchange() {
        return new TopicExchange("businessExchange");
    }

    @Bean
    public Queue ttl10Queue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 10000); // 对象中消息的超时时间是10s
        args.put("x-dead-letter-exchange", "dle-exchange"); // 死信交换机
        args.put("x-dead-letter-routing-key", "dle.err"); // 死信路由键
        return new Queue("10-ttl-queue", true, false, false, args);
    }

    @Bean
    public Queue ttl5Queue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 5000); // 对象中消息的超时时间是10s
        args.put("x-dead-letter-exchange", "dle-exchange"); // 死信交换机
        args.put("x-dead-letter-routing-key", "dle.err"); // 死信路由键
        return new Queue("5-ttl-queue", true, false, false, args);
    }


    @Bean
    public Queue maxLengthQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-max-length", 20); // 对象中消息的超时时间是10s
        args.put("x-dead-letter-exchange", "dle-exchange"); // 死信交换机
        args.put("x-dead-letter-routing-key", "dle.err"); // 死信路由键
        return new Queue("maxlength-queue", true, false, false, args);
    }

    @Bean
    public Binding maxLengthQueueBind() {
        return BindingBuilder.bind(maxLengthQueue()).to(businessExchange()).with("max.*");
    }

    @Bean
    public Binding ttl5QueueBind() {
        return BindingBuilder.bind(ttl5Queue()).to(businessExchange()).with("ttl.5.*");
    }

    @Bean
    public Binding ttl10QueueBind() {
        return BindingBuilder.bind(ttl10Queue()).to(businessExchange()).with("ttl.10.*");
    }

    // 声明业务队列A
    @Bean
    public Queue businessQueue() {
        Map<String, Object> args = new HashMap<>();
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "dle-exchange");
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", "dle.err");

        return new Queue("businessQueue", true, false, false, args);
    }

    // 声明业务队列A绑定关系
    @Bean
    public Binding businessBinding(Queue businessQueue, TopicExchange businessExchange) {
        return BindingBuilder.bind(businessQueue).to(businessExchange).with("emp.*");
    }

}
